package test;
import com.alibaba.druid.pool.DruidDataSource;
import pojo.book;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;



/**
 * @Author: Liudao
 * @Date: 2022-08-04-19:05
 * @Description:
 */
public class springtest {
    
    @Test
    public void test1(){
        //加载spring容器
        ClassPathXmlApplicationContext cpxc = new ClassPathXmlApplicationContext("spring/bean_01.xml");
        //获得容器内的bean
        book book1 = (book) cpxc.getBean("book1");
        System.out.println(book1);
        book book2 = (book) cpxc.getBean("book1");
        System.out.println(book2);      //若bean的制造方式是单例，book1 = book2 若为多例，则这是两本不同的书
        cpxc.close();
    }
    //为属性注入值
    @Test
    public void test2(){
        ClassPathXmlApplicationContext cpxc = new ClassPathXmlApplicationContext("spring/bean_02.xml");
        book book1 = (book) cpxc.getBean("book1");//通过setter方法
        book book2 = (book) cpxc.getBean("book2");//通过构造器
        book book3 = (book) cpxc.getBean("book3");//通过p名称空间
        System.out.println(book1+"\n"+book2+"\n"+book3);
    }
    //创建对象的方法
    @Test
    public void test3(){
        ClassPathXmlApplicationContext classPathXmlApplicationContext = new ClassPathXmlApplicationContext("spring/bean_01.xml");
        //使用静态工厂方法
        Object book2 = classPathXmlApplicationContext.getBean("book2");
        System.out.println(book2);
        //使用工厂方法
        Object book3 = classPathXmlApplicationContext.getBean("book3");
        System.out.println(book3);
    }
    //复杂属性值的引入
    @Test
    public void test4(){
        ClassPathXmlApplicationContext cpxc = new ClassPathXmlApplicationContext("spring/bean_03.xml");
        Object ss = cpxc.getBean("ss");
        System.out.println(ss);
    }
    //利用属性文件读取数据库连接配置
    @Test
    public void test5(){
        ClassPathXmlApplicationContext classPathXmlApplicationContext = new ClassPathXmlApplicationContext("spring/bean_04.xml");
        DruidDataSource dataSource = (DruidDataSource) classPathXmlApplicationContext.getBean("dataSource");
        System.out.println(dataSource.getDriverClassName()+dataSource.getUrl()+dataSource.getUsername()+dataSource.getPassword());
    }
}
